﻿using LeHouse.Server.Dal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace LeHouse.Server.Service
{
    public abstract class BaseService<T>
    {
        /// <summary>
        /// 需要通过工厂创建的DAL对象，写成abstract属性，继承类必须实现
        /// </summary>
        protected abstract GenericBaseDAL<T> Dal
        {
            get;
        }

        #region 通用查询

        public bool Insert(T obj)
        {
            return Dal.Insert(obj);
        }

        /// <summary>
        /// 插入数据（通用方法）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="obj">实例</param>
        /// <param name="index">最后插入ID</param>
        /// <returns></returns>
        public bool Insert(T obj, out long index)
        {
            return Dal.Insert(obj, out index);
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public bool InsertAll(IEnumerable<T> dtos)
        {
            return Dal.InsertAll(dtos);
        }

        public bool Update(T obj)
        {
            return Dal.Update(obj);
        }
        public bool Update(T obj, Expression<Func<T, bool>> Where)
        {
            return Dal.Update(obj, Where);
        }

        public bool UpdateOnly(T obj, Expression<Func<T, object>> onlyFields)
        {
            return Dal.UpdateOnly(obj, onlyFields);
        }

        public List<T> Select()
        {
            return Dal.Select();
        }

        public List<T> Select(Expression<Func<T, bool>> predicate)
        {
            return Dal.Select(predicate);
        }
        /// <summary>
        /// 通用分页查询
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public List<T> SelectByPager(int pageIndex, int pageSize, out long totalSize)
        {
            return Dal.SelectByPager(pageIndex, pageSize, out totalSize);
        }

        public List<T> SelectByIds(System.Collections.IEnumerable ids)
        {
            return Dal.SelectByIds(ids);
        }

        public T SelectById(int id)
        {
            return Dal.SelectById(id);
        }

        public bool DeleteById(int id)
        {
            return Dal.DeleteById(id);
        }

        #endregion
    }
}
